#This file contains the following sections of data analysis:
# 1) Descriptive statistics (Appendix Section A)

# 1) Descriptive statistics ####

# 1.1) Table A1 ####

desc_table <- dat %>%
  mutate(vote_share_change = as.numeric(vote_share_change),
         stc_tba_change_binDecrease = as.numeric(stc_tba_change_bin == "Decrease"),
         stc_tba_change_binStable = as.numeric(stc_tba_change_bin == "Stable"),
         stc_tba_change_binIncrease = as.numeric(stc_tba_change_bin == "Increase"),
         relaxed = as.numeric(relaxed)-1,
         lr_catFL = as.numeric(lr_cat == "Far-Left"),
         lr_catL = as.numeric(lr_cat == "Left"),
         lr_catC = as.numeric(lr_cat == "Center"),
         lr_catR = as.numeric(lr_cat == "Right"),
         lr_catFR = as.numeric(lr_cat == "Far-Right")) %>%
  psych::describe(.) %>%
  add_rownames(., var = "variable") %>% 
  filter(variable %in% c("vote_share_change", "stc_tba_change_binDecrease", "stc_tba_change_binStable", "stc_tba_change_binIncrease", "relaxed", "lr_catFL", "lr_catL", "lr_catC", "lr_catR", "lr_catFR")) %>%
  mutate(Variable = car::recode(variable, "'vote_share_change' = 'Change in vote share';
                                'stc_tba_change_binDecrease' = 'Decreasing STC';
                                'stc_tba_change_binStable' = 'Stable STC';
                                'stc_tba_change_binIncrease' = 'Increasing STC';
                                'relaxed' = 'Clarity of responsibility high';
                                'lr_catFL' = 'Political Ideology Far-Left';
                                'lr_catL' = 'Political Ideology Left';
                                'lr_catC' = 'Political Ideology Center';
                                'lr_catR' = 'Political Ideology Right';
                                'lr_catFR' = 'Political Ideology Far-Right'"),
         order = car::recode(variable, "'vote_share_change' = 1; 'stc_tba_change_binDecrease' = 2; 'stc_tba_change_binStable' = 3;
                             'stc_tba_change_binIncrease' = 4; 'relaxed' = 5; 'lr_catFL' = 6; 'lr_catL' = 7; 'lr_catC' = 8;
                             'lr_catR' = 9; 'lr_catFR' = 10")) %>%
  dplyr::arrange(., order) %>%
  dplyr::select(Variable = Variable, 
                N = n,
                Mean = mean,
                SD = sd,
                Min = min,
                Max = max) 

desc_out <- xtable::xtable(desc_table, 
                           label = "tab:descriptives",
                           digits = c(0, 0, 0, 2, 2, 2, 2), 
                           align = c("l", "l", "r", "r", "r", "r", "r"),
                           caption = "Descriptive statistics of variables")

print(desc_out, 
      include.rownames = F,
      type="latex", file="./output/TableA1.tex", label = "TableA1") 

rm(list=setdiff(ls(), c("dat", "run_start", "run_start_total")))
